package mixing

import (
	"fmt"
	"net/http"
	"strconv"

	"gitee.com/wangshh_123/cross-pay-go/src/database"
	"gitee.com/wangshh_123/cross-pay-go/src/models"
	"gitee.com/wangshh_123/cross-pay-go/src/service"
	"gitee.com/wangshh_123/cross-pay-go/src/utils/common"
	"gitee.com/wangshh_123/cross-pay-go/src/utils/common/i18n"

	"github.com/gin-gonic/gin"
)

// CardFeeController 卡费控制器
type CardFeeController struct {
	cardFeeService *service.CardFeeService
}

// NewCardFeeController 创建卡费控制器
func NewCardFeeController() *CardFeeController {
	return &CardFeeController{
		cardFeeService: service.NewCardFeeService(database.DB),
	}
}

// GetCardFee 获取卡费配置
func (c *CardFeeController) GetCardFee(ctx *gin.Context) {
	cardType := ctx.Query("card_type")
	cardFormFactor := ctx.Query("card_form_factor")
	currency := ctx.Query("currency")

	// 验证参数
	if cardType == "" || cardFormFactor == "" || currency == "" {
		common.Error(ctx, http.StatusBadRequest, "missing_required_params", nil)
		return
	}

	// 验证卡类型
	if cardType != "recharge" && cardType != "share" {
		common.Error(ctx, http.StatusBadRequest, "invalid_card_type", nil)
		return
	}

	// 验证卡形态
	if cardFormFactor != "standard" && cardFormFactor != "premium" && cardFormFactor != "enterprise" {
		common.Error(ctx, http.StatusBadRequest, "invalid_card_form_factor", nil)
		return
	}

	// 验证币种
	validCurrencies := map[string]bool{
		"USD": true,
		"EUR": true,
		"GBP": true,
		"CNH": true,
	}
	if !validCurrencies[currency] {
		common.Error(ctx, http.StatusBadRequest, "invalid_currency", nil)
		return
	}

	fee, err := c.cardFeeService.GetCardFee(ctx, cardType, cardFormFactor, currency)
	if err != nil {
		if err.Error() == "record not found" {
			msg := i18n.GetMessages("card_fee_config_not_found")
			common.Error(ctx, http.StatusNotFound, gin.H{
				"cn": fmt.Sprintf(msg.CN, cardType, cardFormFactor, currency),
				"en": fmt.Sprintf(msg.EN, cardType, cardFormFactor, currency),
				"vn": fmt.Sprintf(msg.VN, cardType, cardFormFactor, currency),
			}, err)
			return
		}
		common.Error(ctx, http.StatusInternalServerError, "card_fee_config_query_failed", err)
		return
	}

	common.Success(ctx, fee)
}

// GetCardFees 获取所有卡费配置
func (c *CardFeeController) GetCardFees(ctx *gin.Context) {
	fees, err := c.cardFeeService.GetCardFees(ctx)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "card_fee_config_list_failed", err)
		return
	}

	common.Success(ctx, fees)
}

// POST /card-fees 新增
func (c *CardFeeController) CreateCardFee(ctx *gin.Context) {
	var req models.CardFeeConfig
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_params", err)
		return
	}
	if err := c.cardFeeService.CreateCardFee(ctx, &req); err != nil {
		common.Error(ctx, http.StatusInternalServerError, "card_fee_config_create_failed", err)
		return
	}
	common.Success(ctx, i18n.GetMessages("card_fee_config_create_success"))
}

// PUT /card-fees/:id 更新
func (c *CardFeeController) UpdateCardFee(ctx *gin.Context) {
	id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
	if err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_id", err)
		return
	}
	var req map[string]interface{}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_params", err)
		return
	}
	if err := c.cardFeeService.UpdateCardFee(ctx, id, req); err != nil {
		common.Error(ctx, http.StatusInternalServerError, "card_fee_config_update_failed", err)
		return
	}
	common.Success(ctx, i18n.GetMessages("card_fee_config_update_success"))
}

// DELETE /card-fees/:id 删除
func (c *CardFeeController) DeleteCardFee(ctx *gin.Context) {
	id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
	if err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_id", err)
		return
	}
	if err := c.cardFeeService.DeleteCardFee(ctx, id); err != nil {
		common.Error(ctx, http.StatusInternalServerError, "card_fee_config_delete_failed", err)
		return
	}
	common.Success(ctx, i18n.GetMessages("card_fee_config_delete_success"))
}

// GET /card-fees/:id 查询单条
func (c *CardFeeController) GetCardFeeByID(ctx *gin.Context) {
	id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
	if err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_id", err)
		return
	}
	config, err := c.cardFeeService.GetCardFeeByID(ctx, id)
	if err != nil {
		common.Error(ctx, http.StatusNotFound, "card_fee_config_not_found", err)
		return
	}
	common.Success(ctx, config)
}
